package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageResult;
import com.oracle.mapper.XsMapper;
import com.oracle.pojo.Xs;
import com.oracle.pojo.XsExample;
import com.oracle.service.XsService;
import com.oracle.vo.XsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class XsServiceImpl implements XsService {
    @Autowired(required = false)
    private XsMapper xsMapper;

    @Override
    public List<Xs> findList() {
        List<Xs> xses = xsMapper.selectByExample(null);
        return xses;
    }

    public PageResult findPages(Integer pageNo, Integer pageSize, Xs xs) {
        PageHelper.startPage(pageNo, pageSize);

        XsExample example = new XsExample();
        XsExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(xs.getXsId())) {
            criteria.andXsIdEqualTo(xs.getXsId());
        }
        if (!ObjectUtils.isEmpty(xs.getXsStudentid())) {
            criteria.andXsStudentidEqualTo(xs.getXsStudentid());
        }
        if (!ObjectUtils.isEmpty(xs.getXsName())) {
            criteria.andXsNameEqualTo(xs.getXsName());
        }
        if (!ObjectUtils.isEmpty(xs.getXsSex())) {
            criteria.andXsSexEqualTo(xs.getXsSex());
        }
        if (!ObjectUtils.isEmpty(xs.getXsAge())) {
            criteria.andXsAgeEqualTo(xs.getXsAge());
        }
        if (!ObjectUtils.isEmpty(xs.getXsPhone())) {
            criteria.andXsPhoneEqualTo(xs.getXsPhone());
        }
        if (!ObjectUtils.isEmpty(xs.getXsZyid())) {
            criteria.andXsZyidEqualTo(xs.getXsZyid());
        }
        if (!ObjectUtils.isEmpty(xs.getXsZyname())) {
            criteria.andXsZynameEqualTo(xs.getXsZyname());
        }
        if (!ObjectUtils.isEmpty(xs.getXsDormitoryid())) {
            criteria.andXsDormitoryidEqualTo(xs.getXsDormitoryid());
        }
        if (!ObjectUtils.isEmpty(xs.getXsBed())) {
            criteria.andXsBedEqualTo(xs.getXsBed());
        }

        example.setOrderByClause("xs_id");

        Page<Xs> xsPage = (Page<Xs>) xsMapper.selectByExample(example);

        List<Xs> xsList = xsPage.getResult();

        List<XsVo> xsVoList = buildXsVoList(xsList);

        return new PageResult(xsPage.getTotal(), xsVoList);
    }

    private List<XsVo> buildXsVoList(List<Xs> xsList) {
        List<XsVo> xsVoList = new ArrayList<>();
        xsList.forEach(xs -> {
            XsVo xsVo2 = new XsVo();

            BeanUtils.copyProperties(xs, xsVo2);

            xsVoList.add(xsVo2);
        });

        return xsVoList;
    }
}
